function XCommanderXCommand() {
	this.initBuildIns();
}

XCommanderXCommand.prototype = {
	_xcommand : null,
	_id : null,
	_generateId : function() {
		return 'xcommand_' + Math.random().toString();
	},
	_getDate : function() {
		var date = new Date();
		return date.toGMTString();
	},
	validate : function(xcommand, callback) {
		try {
			this._xcommand = xcommand;
			eval(xcommand);
			callback({
				'type' : 'valid',
				'key' : this._id,
				'xcommand' : xcommander.lib.storage.getJSONItem(this._id)
			})
		} catch(e) {
			callback({
				"type" : "invalid",
				"error" : e
			});
		}
	},
	create : function(data, buildIn) {
		var xcommand = {
			'meta' : data,
			'buildIn' : buildIn || false,
			'enabled' : true,
			'date' : this._getDate(),
			'xcommand' : this._xcommand
		}
		// check required properties
		if(!data.hasOwnProperty("name")) {
			throw "name property is not implemented!";
		}
		if(!data.hasOwnProperty("version")) {
			throw "version property is not implemented!";
		}
		if(!data.hasOwnProperty("description")) {
			throw "description property is not implemented!";
		}
		if(!data.hasOwnProperty("help")) {
			throw "help property is not implemented!";
		}
		if(!data.hasOwnProperty("author")) {
			throw "author property is not implemented!";
		}
		if(!data.author.hasOwnProperty("name")) {
			throw "author name property is not implemented!";
		}
		if(!data.hasOwnProperty("icon")) {
			throw "icon property is not implemented!";
		}
		if(!data.hasOwnProperty("execute")) {
			throw "execute function is not implemented!";
		}
		if(!data.hasOwnProperty("preview")) {
			throw "preview function is not implemented!";
		}
		if(data.hasOwnProperty("install")) {
			data.install();
		}
		this._id = this._generateId();
		xcommander.lib.storage.setJSONItem(this._id, xcommand);
	},
	getAll : function(callback) {
		var xcommandList = [];
		var keys = xcommander.lib.storage.getKeys()
		for(i in keys) {
			if(keys[i].match('^' + 'xcommand_')) {
				var xcommand = xcommander.lib.storage.getJSONItem(keys[i]);
				xcommandList.push({
					'key' : keys[i],
					'xcommand' : xcommand
				});
			}
		}
		callback(xcommandList);
	},
	getEnabled : function(callback) {
		var xcommandList = [];
		var keys = xcommander.lib.storage.getKeys()
		for(i in keys) {
			if(keys[i].match('^' + 'xcommand_')) {
				var xcommand = xcommander.lib.storage.getJSONItem(keys[i]);
				if(xcommand.enabled) {
					xcommandList.push(xcommand);
				}
			}
		}
		callback(xcommandList);
	},
	initBuildIns : function() {
		var self = this;
		var builtIns = xcommander.lib.storage.getItem("builtIns")
		if(builtIns != "load") {
			var builtInCommands = {
				'xcommander://xcommander/xcommands/options.xcmd' : 'options',
				'xcommander://xcommander/xcommands/help.xcmd' : 'help',
				'xcommander://xcommander/xcommands/set item.xcmd' : 'set item',
				'xcommander://xcommander/xcommands/get item.xcmd' : 'get item'
			};
			for(url in builtInCommands) {
				xcommander.lib.xhr.send({
					'url' : url,
					'type' : 'text'
				}, function(data) {
					self.validate(data.response, function() {

					});
				});
			}
			xcommander.lib.storage.setItem("builtIns", "load");
		}
	}
}